#include "box.hh"

BoundingBox::BoundingBox (const Vector3f& pos, const Vector3f& size)
  : Bounding (pos, Vector3d (), size)
{
}

int BoundingBox::operator() ()
{
  return BOX;
}

bool BoundingBox::intersect (Bounding* obj)
{
  if ((*obj) () == SPHERE)
  {
    return false;
  }
  else if ((*obj) () == BOX)
  {
    double xmin_ = pos_ (0) - size_ (0);
    double xmax_ = pos_ (0) + size_ (0);
    double ymin_ = pos_ (1) - size_ (1);
    double ymax_ = pos_ (1) + size_ (1);
    double zmin_ = pos_ (2) - size_ (2);
    double zmax_ = pos_ (2) - size_ (2);

    double x2min_ = obj->pos () (0) - obj->size () (0);
    double x2max_ = obj->pos () (0) + obj->size () (0);
    double y2min_ = obj->pos () (1) - obj->size () (1);
    double y2max_ = obj->pos () (1) + obj->size () (1);
    double z2min_ = obj->pos () (2) - obj->size () (2);
    double z2max_ = obj->pos () (2) - obj->size () (2);

    double res = (xmin_ - x2min_);
    double res2 = (xmin_ - x2max_);
    double res3 = (xmax_ - x2max_);
    double res4 = (xmax_ - x2min_);

    int cpt = 0;

    if (!((res < 0 && res2 < 0 && res3 < 0 && res4 < 0) ||
         (res > 0 && res2 > 0 && res3 > 0 && res4 > 0)))
      ++cpt;

    res = (ymin_ - y2min_);
    res2 = (ymin_ - y2max_);
    res3 = (ymax_ - y2max_);
    res4 = (ymax_ - y2min_);

    if (!((res < 0 && res2 < 0 && res3 < 0 && res4 < 0) ||
         (res > 0 && res2 > 0 && res3 > 0 && res4 > 0)))
      ++cpt;

    res = (zmin_ - z2min_);
    res2 = (zmin_ - z2max_);
    res3 = (zmax_ - z2max_);
    res4 = (zmax_ - z2min_);

    if (!((res < 0 && res2 < 0 && res3 < 0 && res4 < 0) ||
         (res > 0 && res2 > 0 && res3 > 0 && res4 > 0)))
      ++cpt;
    if (cpt == 3)
      std::cout << "Me toucha\n";

    return false;
  }
  return false;
}

bool
BoundingBox::intersectWall (Plane& p)
{
  p = p;
  return true;
}
